package traditional.thread.communicate;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 利用张孝祥的思路改写
 * @author Germmy
 *
 */
public class TraditionalTheadCommuniceTestGermmy {

	

	public static void main(String[] args) {

		final TraditionalTheadCommuniceTestGermmy ttc = new TraditionalTheadCommuniceTestGermmy();

		final Business business=new Business();
		
		// 整体循环50次
		for (int i = 0; i < 50; i++) {

			new Thread(new Runnable() {

				public void run() {
					// 大哥来搞100次
					business.main();

				}
			}).start();

			new Thread(new Runnable() {

				public void run() {
					// 小弟来搞10次
					business.sub();

				}
			}).start();

			System.out.println("主循环次数:" + i);

		}

	}

	
	 
	 
	 static class Business{
		 boolean isShouldBoss = false;
		 Lock lock=new ReentrantLock();
		 void main() {
			 	lock.lock();
				while (isShouldBoss) {
					for (int i = 0; i < 100; i++) {
						System.out.println("main:" + i);
					}
					isShouldBoss = false;
				}
				try {
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lock.unlock();
			}

			 void sub() {
				 lock.lock();
				while (!isShouldBoss) {
					for (int i = 0; i < 10; i++) {
						System.out.println("sub:" + i);
					}
					isShouldBoss = true;
				}
				try {
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lock.unlock();
			}
			 
	 }
	 
	 
	 
	 

}
